package cn.stylefeng.guns.goodsdetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.goods.entity.Goods;
import cn.stylefeng.guns.goodsdetail.dto.GoodsDetailDTO;
import cn.stylefeng.guns.goodsdetail.entity.GoodsDetail;
import cn.stylefeng.guns.goodsdetail.mapper.GoodsDetailMapper;
import cn.stylefeng.guns.goodsdetail.service.GoodsDetailService;
import cn.stylefeng.guns.goodsdetail.enums.GoodsDetailExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * goods_detail 服务实现类
 *
 * @author aria
 * @date 2021/02/20 20:24
 */
@Service
public class GoodsDetailServiceImpl extends ServiceImpl<GoodsDetailMapper, GoodsDetail> implements GoodsDetailService {

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    public PageResult<GoodsDetail> page(GoodsDetailDTO goodsDetailDTO) {

        // 构造条件
        LambdaQueryWrapper<GoodsDetail> queryWrapper = new LambdaQueryWrapper<>();

        // 拼接查询条件-
        if (goodsDetailDTO != null && ObjectUtil.isNotEmpty(goodsDetailDTO.getDescId())) {
            queryWrapper.like(GoodsDetail::getDescId, goodsDetailDTO.getDescId());
        }

        // 拼接查询条件-
        if (goodsDetailDTO != null && ObjectUtil.isNotEmpty(goodsDetailDTO.getGoodsId())) {
            queryWrapper.like(GoodsDetail::getGoodsId, goodsDetailDTO.getGoodsId());
        }

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<GoodsDetail> list(GoodsDetailDTO goodsDetailDTO) {

        // 构造条件
        LambdaQueryWrapper<GoodsDetail> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(GoodsDetailDTO goodsDetailDTO) {

        // 将dto转为实体
        GoodsDetail goodsDetail = new GoodsDetail();
        BeanUtil.copyProperties(goodsDetailDTO, goodsDetail);

        this.save(goodsDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(GoodsDetailDTO goodsDetailDTO) {
        this.removeById(goodsDetailDTO.getDescId());
    }

    @Override
    public void edit(GoodsDetailDTO goodsDetailDTO) {

        // 根据id查询实体
        GoodsDetail goodsDetail = this.queryGoodsDetail(goodsDetailDTO);

        // 请求参数转化为实体
        BeanUtil.copyProperties(goodsDetailDTO, goodsDetail);

        this.updateById(goodsDetail);
    }

    @Override
    public void update(GoodsDetail goodsDetail) {
        this.updateById(goodsDetail);
    }

    @Override
    public GoodsDetail detail(GoodsDetailDTO goodsDetailDTO) {
        return this.queryGoodsDetail(goodsDetailDTO);
    }

    @Override
    public List<GoodsDetail> getListByGoodsId(Goods goods) {
        return goodsDetailMapper.getListByGoodId(goods.getGoodsId());
    }

    @Override
    public void insert(GoodsDetail goodsDetail) {
        baseMapper.insert(goodsDetail);
    }


    /**
     * 获取goods_detail
     *
     * @author aria
     * @since 2021/02/20 20:24
     */
    private GoodsDetail queryGoodsDetail(GoodsDetailDTO goodsDetailDTO) {
        GoodsDetail goodsDetail = this.getById(goodsDetailDTO.getDescId());
        if (ObjectUtil.isEmpty(goodsDetail)) {
            throw new ServiceException(GoodsDetailExceptionEnum.NOT_EXISTED);
        }
        return goodsDetail;
    }
}